package com.example.yskjproject.base;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewbinding.ViewBinding;

import com.example.yskjproject.R;
import com.example.yskjproject.ScreenAdapter;
import com.example.yskjproject.utils.EventBusUtil;
import com.example.yskjproject.view.dialog.MyProgressDialog;

public abstract class BaseFragment<T extends ViewBinding> extends Fragment {

    private T binding;
    MyProgressDialog custom1;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = onCreateViewBinding(inflater, container);
        if (isRegisterEventBus()) {
            EventBusUtil.register(this);
        }
        custom1 = new MyProgressDialog(getActivity(), R.style.CustomTheme);//R.style.CustomTheme自定义加载动画圈的颜色
        return binding.getRoot();
    }

    protected void isShow(boolean isShow) {
        if (isShow) {
            custom1.show();
        }else {
            custom1.hide();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        onData();
    }

    protected abstract T onCreateViewBinding(@NonNull LayoutInflater inflater, @Nullable ViewGroup parent);

    protected abstract void onData();

    protected boolean isRegisterEventBus() {
        return false;
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        binding = null;
        if (isRegisterEventBus()) {
            EventBusUtil.unregister(this);
        }
    }
}
